﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports Microsoft.VisualBasic.CompilerServices
Imports System.Web.Script.Serialization
Imports System.Web.Script.Services

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<ScriptService()> _
Public Class WSJason
    Inherits System.Web.Services.WebService

    Dim connstr As String = ConfigurationManager.ConnectionStrings("MySQLConnString").ConnectionString



    <WebMethod()> _
    <Script.Services.ScriptMethod(ResponseFormat:=System.Web.Script.Services.ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=False)> _
    Public Function HelloWorld() As String
        Dim js As JavaScriptSerializer = New JavaScriptSerializer()
        Dim sJSON As String = "Hello World"
        Return sJSON
    End Function

    <WebMethod()> _
    <Script.Services.ScriptMethod(ResponseFormat:=System.Web.Script.Services.ResponseFormat.Json, UseHttpGet:=True, XmlSerializeString:=False)> _
    Public Function GetHelloWithParam(ByVal vInputName As String) As String
        Return "Hello, " + vInputName
    End Function

    <WebMethod(Description:="Display data from main table. (JSON format)")> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function DisplayDataJson() As String
        Dim connstr As String = ConfigurationManager.ConnectionStrings("MySQLConnString").ConnectionString
        Dim cnMySQL As MySqlConnection = New MySqlConnection(connstr)
        cnMySQL.Open()
        Dim cmdMySQL As MySqlCommand = New MySqlCommand("select IDrec, user, points, rounds from bullseye")
        cmdMySQL.Connection = cnMySQL
        Dim ds As DataSet = New DataSet
        Dim mySQLadapter As MySqlDataAdapter = New MySqlDataAdapter(cmdMySQL)
        mySQLadapter.Fill(ds)
        cnMySQL.Close()

        Dim MArray()() As String = New String(ds.Tables(0).Rows.Count)() {}
        Dim i As Integer = 0
        For Each rs As DataRow In ds.Tables(0).Rows
            MArray(i) = New String() {rs("IDrec").ToString(), rs("user").ToString(), rs("points").ToString(), rs("rounds").ToString()}
            i = i + 1
        Next

        Dim js As JavaScriptSerializer = New JavaScriptSerializer()
        Dim sJSON As String = js.Serialize(MArray)
        Return sJSON
    End Function

End Class